/* Utils for beacon data.
* Neil Z. Shao
*/
#pragma once
#include <queue>
#include <vector>
#include <stdio.h>
#include <string>
#include <algorithm>
namespace easyshow
{
	///////////////////////////////////// sort /////////////////////////////////////
	// Sort and get the sorted index
	template<typename T>
	struct less_cmp {
		less_cmp(const T varr) : arr(varr) {}
		inline bool operator()(const size_t a, const size_t b) const
		{
			return arr[a] < arr[b];
		}
		const T arr;
	};

	template<typename T>
	struct larger_cmp {
		larger_cmp(const T varr) : arr(varr) {}
		inline bool operator()(const size_t a, const size_t b) const
		{
			return arr[a] > arr[b];
		}
		const T arr;
	};

	template<typename T>
	void sortIndex(const std::vector<T> &vals, std::vector<int> &idx, bool bAscending = true)
	{
		if (vals.empty())
			return;

		if (idx.empty() || idx.size() != vals.size()) {
			idx.clear();
			for (int i = 0; i < vals.size(); idx.push_back(i++));
		}

		if (bAscending)
			sort(idx.begin(), idx.end(), less_cmp<const std::vector<T> &>(vals));
		else
			sort(idx.begin(), idx.end(), larger_cmp<const std::vector<T> &>(vals));
	}

	template<typename T>
	std::vector<int> sortIndex(const std::vector<T> &vals, bool bAscending = true)
	{
		if (vals.empty())
			return std::vector<int>();

		std::vector<int> idx(vals.size());
		for (int i = 0; i < vals.size(); ++i)
			idx[i] = i;
		sortIndex(vals, idx, bAscending);

		return idx;
	}

	///////////////////////////////////// math /////////////////////////////////////
	inline float sqrf(float value)
	{
		return value * value;
	}

	///////////////////////////////////// median /////////////////////////////////////
	float findMedian(const std::deque<float> &values);

	///////////////////////////////////// string /////////////////////////////////////
	// make string out of int
	std::string toString(int num, int n_digits = 0);
	std::string toString(double num, int n_digits = 0);
}
